import HttpUtils from '../HttpUtils/HttpUtils';
Page({
	...HttpUtils.func,
	data: {
		colletIcon: "../../img/collectICON.png",
		collectStatus: "no",
		news: {
		},
		name: '',
	},
	onLoad(params) {
		var newsId = params.noticeId;
		var name = params.name;
		this.setData({
			newsId: newsId,
			name: name,
		});
		this.loadNoticeInfo();
	},
	loadNoticeInfo: function() {
		var params = "{'message': {'version': '1.0','header': {'action': 'REQUEST','command': 'PARTY_NOTICE_INFO','sequence': '10000000001'},'body': {'news': {'partyId': " + getApp().globalData.partyId + ",'userId': " + getApp().globalData.userId + ",'newsId': " + this.data.newsId + "}}}}";
		this.httpGet(getApp().globalData.dangjianUrl, params, "通知公告详情加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var news = respData.data.news;
					news.publishTime = this.formatDate("yyyy-MM-dd", news.publishTime);
					this.setData({
						"news": news
					});
					//查询是否收藏
					this.loadFavor();
				} else {
					dd.alert({ title: respData.data.result.description });
				}
			}
		);
	},

	loadFavor: function() {
		//支部内容是否收藏
		var newUrl = getApp().globalData.dangjianUrl + "/member/userContentIsFavor";
		// var params = '"code":"' + this.data.newsId +
		// 	'","userId":"' + getApp().globalData.userId +
		// 	'","contentType":"2",corpId":"' + getApp().globalData.corpId + '",'
		// 	+ '"sign":"appKey=1&code=' + this.data.parentId + '&nonce=' + timestamp + '&siteCode=' + getApp().globalData.siteCode + '&timestamp=' + timestamp + '&appSecret=' + getApp().globalData.appSecret + '"}';
		var params = {};
		params["code"] = this.data.newsId + "";
		params["name"] = this.data.name + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "通知公告是否收藏调用失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var isFavor = respData.data.favor.isFavor;
					//默认为收藏的状态
					var favorIcon = "../../img/collectFillICON.png";
					if (isFavor == "false") {
						favorIcon = "../../img/collectICON.png";
					}
					this.setData({
						"favorIcon": favorIcon,
						"isFavor": isFavor
					});
				} else {
					dd.alert({ title: respData.data.result.description });
				}
			}
		);
	},
	favorContent: function(e) {
		//收藏内容
		var status = e.currentTarget.dataset.status;
		var contentName = e.currentTarget.dataset.name;
		status = "false" == status ? 1 : 0;
		var newUrl = getApp().globalData.dangjianUrl + "/member/userContentFavor";
		var params = {};
		params["code"] = this.data.newsId + "";
		params["siteCode"] = "";
		params["status"] = status + "";
		params["contentName"] = contentName + "";
		params["contentType"] = "2";
		params["columnCode"] = "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "收藏通知公告内容失败").then(
			(respData) => {
				var result = respData.data.result.code;
				var isFavor = "true";
				var favorIcon = "../../img/collectFillICON.png";
				if (1 == result) {
					if (0 == status) {
						favorIcon = "../../img/collectICON.png";
						isFavor = "false";
						this.showPageMsg("tips", "该内容收藏取消成功！");
					} else {
						this.showPageMsg("tips", "该内容收藏成功！");
					}
					this.setData({
						"favorIcon": favorIcon,
						"isFavor": isFavor
					});
				} else {
					dd.alert({ title: respData.data.result.description });
				}
			}
		);
	}
});
